﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.IBLL.BaseSystem;
using Base.Core.UnityFactory;
using BF.Model;
using System.Web.Security;

namespace BF.Web.pages.BaseSystem.UserManagement
{
    public partial class ChangePassword : BasePage
    {
        #region 实例
        Isys_UserInfoBLL iBLL = IOCFactory.GetIOCResolve<Isys_UserInfoBLL>();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindPager();
            }
        }

        protected void BindPager()
        {
            if (!String.IsNullOrEmpty(GetSessionCurrentUserInfo.sLoginName))
            {
                this.userName.Text = GetSessionCurrentUserInfo.sLoginName;
            }
            else
            {
                Response.Redirect("/login.aspx");
            }
        }

        protected void tbnSave_Click(object sender, EventArgs e)
        {
            //验证用户密码是否正确
            if (this.CheckUserAndPassword(this.userName.Text.Trim(), this.tbxPssword.Text.Trim()))
            {
                if (this.UserChangePassword(this.userName.Text.Trim(), this.tbxPssword.Text.Trim(), this.tbxnewpwd.Text.Trim()))
                {
                    sys_UserInfo userInfo = iBLL.GetModelByloginname(this.userName.Text.Trim());
                    userInfo.sPassword = this.tbxnewpwd.Text.Trim();
                    iBLL.Update();
                    Messages("密码修改成功！");
                }
                else
                {
                    Messages("密码修改失败！");
                    return;
                }
            }
            else
            {
                Messages("您输入的原始密码不正确，请重新输入！");
                return;
            }
        }

        #region 验证用户登陆名密码是否正确
        /// <summary>
        /// 验证用户登陆名密码是否正确
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="?"></param>
        /// <returns></returns>
        private bool CheckUserAndPassword(string UserName, string UserPassword)
        {
            bool flag = false;
            if (System.Web.Security.Membership.ValidateUser(UserName, UserPassword))
            {
                flag = true;
            }
            return flag;
        }
        #endregion

        #region 修改密码
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="oldPassword">旧密码</param>
        /// <param name="newPassword">新密码</param>
        /// <returns></returns>
        private bool UserChangePassword(string userName, string oldPassword, string newPassword)
        {
            bool flag = false;
            MembershipUser myShip = System.Web.Security.Membership.GetUser(userName);
            if (myShip.ChangePassword(oldPassword, newPassword))
            {
                flag = true;
            }
            return flag;
        }
        #endregion

    }
}